//
//  AppDelegate.swift
//  Swiftsweibo
//
//  Created by 张鹏飞 on 15/12/31.
//  Copyright © 2015年 张鹏飞. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    
    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //测试归档数据
       UserAccountViewModel.sharedUserAccount
        setUpAppearance()
        window = UIWindow(frame: UIScreen .mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        
        window?.rootViewController = defaultRootViewController
        
        window?.makeKeyAndVisible()
        
        //监听通知
        NSNotificationCenter.defaultCenter().addObserverForName(WBSwitchRootViewControllerNotification,
            object: nil,
            queue: nil) { [weak self] (notification) -> Void in
                
                let vc = notification.object != nil ? WelComeViewController() : MainViewController()
                //切换控制器
                self?.window?.rootViewController = vc
        }
        
        return true
    }
    //注销通知
    deinit {
    
        NSNotificationCenter.defaultCenter().removeObserver(self,  //监听者
            name: WBSwitchRootViewControllerNotification,          //通知名
            object: nil)                                           //发送通知的对象
    }
/// 设置全局外观
    
    private func setUpAppearance(){
    
       UINavigationBar.appearance().tintColor = WBAppearanceTintColor
        UITabBar.appearance().tintColor = WBAppearanceTintColor
    }

}
/// 界面切换代码
extension AppDelegate {
    
 /// 启动的跟视图控制器
    private var defaultRootViewController: UIViewController {
    
        //判断是否登录
        if UserAccountViewModel.sharedUserAccount.userLogon {
        
            return isNewVersion ? NewFeatureViewController() : WelComeViewController()
        }
        //2.没有登录返回主控制器
        return MainViewController()
    }

/// 判断是否是新版本
    //计算型属性,extension中不能写存储型属性
    private var isNewVersion: Bool {
    
        //当前版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let version = Double(currentVersion)!
        
        //之前的版本
        
        let standBoxVersionKey = "standBoxVersionKey"
        let standBoxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(standBoxVersionKey)
        print("之前版本\(standBoxVersion)")
        //将当前版本存入用户偏好
        NSUserDefaults.standardUserDefaults().setDouble(version, forKey: standBoxVersionKey)
        
        return version > standBoxVersion
        
        return true
    }
}

